package com.hu.config;


import com.hu.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandlerInterceptor implements HandlerInterceptor {

    public static volatile ThreadLocal<Integer> useIdLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token  =  request.getHeader("Authorization");
        System.out.println(token);
        if (token == null ||!TokenUtil.isVerify(token)){
            response.setStatus(401);
            return false;
        }else {
            Claims claims = TokenUtil.parseJWT(token);
            useIdLocal.set((Integer) claims.get("userId"));
        }
        return true;
    }
}
